﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.DAL;

public partial class Police_Select_SelectList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserGrid.Caption = "<span>未审核士兵库</span>";
            ListPageContral1.SetFirstPage();
            BindGV(0);
        }
    }

    void BindGV(int pageIndex)
    {
        //调出时不显示联系人人人

        if (rblTpye.SelectedValue == "1")
        {
            UserGrid.Columns[8].Visible = false;
            if (ddlsort.Items.Contains(new ListItem("联系人", "LinkMen")))
            {
                ddlsort.Items.Remove(new ListItem("联系人", "LinkMen"));
            }
        }
        else
        {
            UserGrid.Columns[8].Visible = true;
            if (!ddlsort.Items.Contains(new ListItem("联系人", "LinkMen")))
            {
                ddlsort.Items.Remove(new ListItem("备注", "Remark"));
                ddlsort.Items.Add(new ListItem("联系人", "LinkMen"));
                ddlsort.Items.Add(new ListItem("备注", "Remark"));
            }
        }
        if (rblTpye.SelectedValue == "2")
        {
            UserGrid.Columns[9].Visible = false;
            UserGrid.Columns[10].Visible = true;
        }
        else
        {
            UserGrid.Columns[9].Visible = true;
            UserGrid.Columns[10].Visible = false;
        }

        List<TranslateSelect> list = new List<TranslateSelect>();
        string where = "";


        where = lblQuery.Text;

        if (ddlsort.SelectedValue == "LastDate")
        {
            list = Select.GetNoTranslateList(where, Convert.ToInt32(rblTpye.SelectedValue), pageIndex, ListPageContral1.PageSize, ddlsort.SelectedValue, "desc");
        }
        else
        {
            list = Select.GetNoTranslateList(where, Convert.ToInt32(rblTpye.SelectedValue), pageIndex, ListPageContral1.PageSize, ddlsort.SelectedValue, "ASC");
        }
        UserGrid.DataSource = list;
        UserGrid.DataBind();
        ListPageContral1.ItemCount = Select.GetNoTranslateCount(where, Convert.ToInt32(rblTpye.SelectedValue));

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindGV(PageIndex);
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            TranslateSelect select = e.Row.DataItem as TranslateSelect;
            Label lblRank = e.Row.FindControl("lblRank") as Label;
            lblRank.Text = select.Rank.Trim() == "" ? "" : Yipusoft.Common.BLL.CodeItem.GetCodeItem(select.Rank).ItemName;

        }
    }
    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void butOP_Click(object sender, EventArgs e)
    {
        SelectDataContext db = new SelectDataContext();
        List<SelectLogs> logs = new List<SelectLogs>();
        foreach (GridViewRow row in UserGrid.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
                if (CheckItem.Checked)
                {

                    SelectLogs log = new SelectLogs();
                    log.Id = Guid.NewGuid();
                    log.SelectId = new Guid(UserGrid.DataKeys[row.RowIndex].Value.ToString());
                    log.SetDate = DateTime.Now;
                    log.SetUserId = User.Identity.Name;
                    log.State = 0;
                    logs.Add(log);
                }
            }
        }

        db.SelectLogs.InsertAllOnSubmit(logs);
        db.SubmitChanges();
        ListPageContral1.SetFirstPage();
        BindGV(0);
        Prompt.Text = "提示：上报" + logs.Count + "人。";
    }
    protected void rblState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        SelectDataContext db = new SelectDataContext();
        List<TranslateSelect> logs = new List<TranslateSelect>();
        foreach (GridViewRow row in UserGrid.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
                if (CheckItem.Checked)
                {

                    TranslateSelect info = db.TranslateSelect.Where(o => o.Id == new Guid(UserGrid.DataKeys[row.DataItemIndex].Value.ToString())).FirstOrDefault();
                    logs.Add(info);
                }
            }
        }

        db.TranslateSelect.DeleteAllOnSubmit(logs);
        db.SubmitChanges();
        ListPageContral1.SetFirstPage();
        BindGV(0);
        Prompt.Text = "提示：删除" + logs.Count + "人。";
    }
    protected void btnUpd_Click(object sender, EventArgs e)
    {
        SelectDataContext db = new SelectDataContext();

        foreach (GridViewRow row in UserGrid.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
                if (CheckItem.Checked)
                {
                    if (rblTpye.SelectedValue == "0")
                    {
                        Response.Redirect("SelectAdd.aspx?id=" + UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
                    }
                    if (rblTpye.SelectedValue == "1")
                    {
                        Guid id = new Guid(UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
                        TranslateSelect info = db.TranslateSelect.Where(o => o.Id == id).FirstOrDefault();
                        Response.Redirect("SelectOut.aspx?id=" + info.SoldierId + "&SelectId=" + UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
                    }
                    if (rblTpye.SelectedValue == "2")
                    {
                        Guid id = new Guid(UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
                        TranslateSelect info = db.TranslateSelect.Where(o => o.Id == id).FirstOrDefault();
                        Response.Redirect("SelectInner.aspx?id=" + info.SoldierId + "&SelectId=" + UserGrid.DataKeys[row.DataItemIndex].Value.ToString());

                    }
                }
            }
        }
    }
    protected void rblTpye_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlsort.SelectedIndex = 0;
        lblQuery.Text = "";
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }
    protected void ddlsort_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        lblQuery.Text = txtQuery.Text;
        txtQuery.Text = "";
        BindGV(0);
    }
}
